<?php

class system extends base {

    private $content;

    public function submenu() {
        $this->submenu = array();
        $this->submenu[0]["name"] = "System";
        $this->submenu[0]["callback"] = "system";
        $this->submenu[0]["icon"] = "icons/cross.png";
        $this->submenu[0]["rights"] = 0;
        $this->submenu[0]["enabled"] = true;

        $this->submenu[1]["name"] = "Network";
        $this->submenu[1]["callback"] = "network";
        $this->submenu[1]["icon"] = "icons/cross.png";
        $this->submenu[1]["rights"] = 0;
        $this->submenu[1]["enabled"] = true;

        $this->submenu[2]["name"] = "Accel";
        $this->submenu[2]["callback"] = "accel";
        $this->submenu[2]["icon"] = "icons/cross.png";
        $this->submenu[2]["rights"] = 0;
        $this->submenu[2]["enabled"] = true;

        $this->submenu[3]["name"] = "VMStat";
        $this->submenu[3]["callback"] = "vmstat";
        $this->submenu[3]["icon"] = "icons/cross.png";
        $this->submenu[3]["rights"] = 0;
        $this->submenu[3]["enabled"] = true;

        $this->submenu[4]["name"] = "Meminfo";
        $this->submenu[4]["callback"] = "meminfo";
        $this->submenu[4]["icon"] = "icons/cross.png";
        $this->submenu[4]["rights"] = 0;
        $this->submenu[4]["enabled"] = true;

        $this->submenu[5]["name"] = "Slabinfo";
        $this->submenu[5]["callback"] = "slabinfo";
        $this->submenu[5]["icon"] = "icons/cross.png";
        $this->submenu[5]["rights"] = 0;
        $this->submenu[5]["enabled"] = false;

        $this->submenu[6]["name"] = "Modules";
        $this->submenu[6]["callback"] = "modules";
        $this->submenu[6]["icon"] = "icons/cross.png";
        $this->submenu[6]["rights"] = 0;
        $this->submenu[6]["enabled"] = true;

        $this->submenu[7]["name"] = "Interrupts";
        $this->submenu[7]["callback"] = "interrupts";
        $this->submenu[7]["icon"] = "icons/cross.png";
        $this->submenu[7]["rights"] = 0;
        $this->submenu[7]["enabled"] = true;

        $this->submenu[8]["name"] = "Process";
        $this->submenu[8]["callback"] = "process";
        $this->submenu[8]["icon"] = "icons/cross.png";
        $this->submenu[8]["rights"] = 0;
        $this->submenu[8]["enabled"] = true;

        $this->_submenu($this->submenu);
    }

    public function run() {
        $this->submenu();
        $callback = $this->_run();
        if ($callback)
            $this->content = $this->$callback();
        $this->finish($this->content);
    }

    public function system() {
        $output = "<h1>Accel-PPPD</h1><br />
        <img src = \"graphs/cpu.png\" />
        <img src = \"graphs/system.png\" /><br />
        <br /><br />";
        return $output;
    }

    public function network() {
        exec("/sbin/ifconfig eth0", $eth0);
        exec("/sbin/ifconfig eth1", $eth1);
        $eth0 = implode("<br>\n", $eth0);
        $eth1 = implode("<br>\n", $eth1);
        $output = "<h1>Accel-PPPD</h1><br />
        <table width=100%>
        <tr>
        <td valign='top'>
        <img src = \"graphs/network.png\" />
        </td>
        <td valign='top'>
        <img src = \"graphs/network1.png\" /><br />
        </td>
        </tr><tr>
        <td>

        {$eth0}
        </td>
        <td>
        {$eth1}
        </td>
        </tr>
        </table>";
        return $output;
    }

    public function accel() {
        $accel = file_get_contents("dumps/status");
        $accel = str_replace("\r", "<br>\r", $accel);
        $output = "<h1>Accel-PPPD</h1><br />
        <img src = \"graphs/accel.png\" />
        <br /><br />
        {$accel}";
        return $output;
    }

    public function vmstat() {
        $vmstat = file_get_contents("/proc/vmstat");
        $stat=explode("\n",$vmstat);
        $table=array();
        foreach ($stat as $pss) {
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $table1=explode(" ",$pss);
            $table[]=$table1;
            }
        $header[] = array("Item","Mem");
        $table_html = rows2table(array_merge($header, $table), true);
        return $table_html;
    }

    public function meminfo() {
        $output = "<h1>Accel-PPPD</h1><br />
        <img src = \"graphs/memory.png\" /><br />
        <br /><br />";
        $stat = file_get_contents("/proc/meminfo");
        $stat1 = explode("\n",$stat);
        $table=array();
        foreach ($stat1 as $pss) {
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $table1=explode(" ",$pss);
            $table[]=$table1;
            }
        $header[] = array("Item","Mem","Unit");
        $table_html = rows2table(array_merge($header, $table), true);
        return $output.$table_html;
    }

    public function slabinfo() {
        $output = file_get_contents("/proc/slabinfo");
        $output = "Slabinfo: <br/>" . str_replace("\n", "<br>", $output) . "<br><br>\n";
        return $output;
    }

    public function modules() {
        $output = file_get_contents("/proc/modules");
        $stat1 = explode("\n",$output);
        $table=array();
        foreach ($stat1 as $pss) {
            $pss=str_replace("  "," ",trim($pss));
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $table1=explode(" ",$pss);
            $table[]=$table1;
            }
        $header[] = array("Name", "Memory" ,"Instances", "Depends", "State", "Offset", "");
        $table_html = rows2table(array_merge($header, $table), true);
        return $table_html;
    }

    public function interrupts() {
        $output = file_get_contents("/proc/interrupts");
        $stat1 = explode("\n",$output);
        $table=array();
        foreach ($stat1 as $pss) {
            $pss=str_replace("  "," ",trim($pss));
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $table1=explode(" ",$pss);
            $table[]=$table1;
            }
        $header[] = array("CPU0", "CPU1" ,"CPU2", "CPU3", "Module", "Module", "Module", "Module");
        $table_html = rows2table(array_merge($header, $table), true);
        return $table_html;
    }

    public function process() {
        exec("/bin/ps aux",$ps);
        //$ps=implode("<br>\n",$ps);
        $table=array();
        foreach ($ps as $pss) {
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $pss=str_replace("  "," ",$pss);
            $table1=explode(" ",$pss);
            if ($table1[0]!="USER")
                $table[]=$table1;
            }
        $header[] = array("USER","PID","%CPU","%MEM","VSZ","RSS","TTY","STAT","START","TIME","COMMAND","PARAM1","PARAM2","PARAM4","PARAM4","PARAM5");
        $table_html = rows2table(array_merge($header, $table), true);
        return $table_html;
    }

}

?>